package com.qfjg.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfjg.VO.ProjectsUidAndPhone;
import com.qfjg.entity.PhoneList;
import com.qfjg.entity.ProjectsInfo;
import com.qfjg.mappers.PhoneListMapper;
import com.qfjg.mappers.ProjectsInfoMapper;
import com.qfjg.service.ProjectsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProjectsInfoImp extends ServiceImpl<ProjectsInfoMapper, ProjectsInfo> implements ProjectsInfoService {

    @Autowired
    private ProjectsInfoMapper projectsInfoMapper;

    @Autowired
    private PhoneListMapper phoneListMapper;

    @Override
    public void insert(ProjectsInfo projectsInfo) {
        projectsInfoMapper.insert(projectsInfo);
    }

    @Override
    public List<ProjectsInfo> getProjectsByUid(String uid) {

        return projectsInfoMapper.getProjectsByUid(uid);

    }

    @Override
    public void delProjectById(String id) {
        projectsInfoMapper.deleteById(id);
    }

    @Override
    public boolean updateById(ProjectsInfo projectsInfo) {
        return projectsInfoMapper.updateById(projectsInfo) > 0;
    }

    @Override
    public List<ProjectsInfo> getProjectsByAdmin(List<String> roles) {
        QueryWrapper<ProjectsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("compt_dept_id", roles)
                .eq("status", "已提交")
                .orderByDesc("create_time");
        return projectsInfoMapper.selectList(queryWrapper);
    }


    @Override
    public List<ProjectsInfo> getProjectsByAdminFilter(ProjectsInfo projectsInfo, ArrayList<String> rids) {
      /*
        System.out.println(projectsInfo);
        QueryWrapper<ProjectsInfo> queryWrapper = new QueryWrapper<>();
        // 默认查‘已提交’的数据
        queryWrapper.eq("status", "已提交");

        // 通过create_time实现排序（时间由近到远）
        queryWrapper.orderByDesc("create_time");

        // 动态模糊查询
        if (isNotEmpty(projectsInfo.getCertificateNumber())) {
            queryWrapper.like("certificate_number", projectsInfo.getCertificateNumber());
        }
        if (isNotEmpty(projectsInfo.getApplierOrg())) {
            queryWrapper.like("applier_org", projectsInfo.getApplierOrg());
        }
        if (isNotEmpty(projectsInfo.getWorkUnit())) {
            queryWrapper.like("work_unit", projectsInfo.getWorkUnit());
        }
        if (isNotEmpty(projectsInfo.getProName())) {
            queryWrapper.like("pro_name", projectsInfo.getProName());
        }
        if (isNotEmpty(projectsInfo.getContacts())) {
            queryWrapper.like("contacts", projectsInfo.getContacts());
        }
        if (isNotEmpty(projectsInfo.getPhoneNumber())) {
            queryWrapper.like("phone_number", projectsInfo.getPhoneNumber());
        }
        if (isNotEmpty(projectsInfo.getDumpingAreaName())) {
            queryWrapper.like("dumping_area_name", projectsInfo.getDumpingAreaName());
        }

        // 动态等值查询
        if (isNotEmpty(projectsInfo.getComptDeptId())) {
            queryWrapper.eq("compt_dept_id", projectsInfo.getComptDeptId());
        } else {
            queryWrapper.in("compt_dept_id", rids);
        }

        if (isNotEmpty(projectsInfo.getDumpingMethod())) {
            queryWrapper.eq("dumping_method", projectsInfo.getDumpingMethod());
        }
        // JSONB 查询处理
        if (projectsInfo.getShipList() != null && !projectsInfo.getShipList().isEmpty() && isNotEmpty(projectsInfo.getShipList().get(0))) {
            queryWrapper.apply("EXISTS (SELECT 1 FROM jsonb_array_elements_text(ship_list::jsonb) AS elem WHERE elem = {0})", projectsInfo.getShipList().get(0));
        }

        if (projectsInfo.getWasteTypes() != null && !projectsInfo.getWasteTypes().isEmpty() && isNotEmpty(projectsInfo.getWasteTypes().get(0))) {
            queryWrapper.apply("EXISTS (SELECT 1 FROM jsonb_array_elements_text(waste_types::jsonb) AS elem WHERE elem = {0})", projectsInfo.getWasteTypes().get(0));
        }
        return projectsInfoMapper.selectList(queryWrapper);
        */
        return projectsInfoMapper.getProjectsByAdminFilter(projectsInfo, rids);
    }


    @Override
    public List<ProjectsInfo> getProjectsByPhoneNumber(String phone) {

        QueryWrapper<PhoneList> phoneListWrapper = new QueryWrapper<>();
        phoneListWrapper.eq("phone_number", phone);
        List<PhoneList> phoneList = phoneListMapper.selectList(phoneListWrapper);

        List<String> projectIds = new ArrayList<>();

        for (PhoneList list : phoneList) {
            projectIds.add(list.getProjectId());
        }

        System.out.println(projectIds);
        if (!projectIds.isEmpty()) {
            QueryWrapper<ProjectsInfo> queryWrapperProjectsInfo = new QueryWrapper<>();
            queryWrapperProjectsInfo.in("project_id", projectIds);
            return projectsInfoMapper.selectList(queryWrapperProjectsInfo);
        }

        return null;
    }

    @Override
    public List<ProjectsInfo> ProjectsUidAndPhone(ProjectsUidAndPhone uidAndPhone) {
        return projectsInfoMapper.getProjectsByUidAndPhone(uidAndPhone.getUid(), uidAndPhone.getPhone());
    }




    private String toJsonbString(String value) {
        return "[\"" + value + "\"]";
    }
}
